View Javadoc
1 //============================================================================== 2 // file : CommonCommand.java 3 // project: East Networks News System 4 // 5 // last change: date: $Date: 2003/09/10 09:28:37 $ 6 // by: $Author: bitiboy $ 7 // revision: $Revision: 1.1 $ 8 //------------------------------------------------------------------------------ 9 // copyright: GNU GPL Software License (see class documentation) 10 //============================================================================== 11 package net.eastol.news.common; 12 13 14 /* 15 * $Id: CommonCommand.java,v 1.1 2003/09/10 09:28:37 bitiboy Exp $ 16 * 17 * Copyright 2003 Acai Software All Rights Reserved. 18 * 19 * This file CommonCommand.java is part of the East Networks News System. 20 21 * The East Networks News System is free software; you can redistribute it and/or modify 22 * it under the terms of the GNU General Public License as published by 23 * the Free Software Foundation; either version 2 of the License, or 24 * (at your option) any later version. 25 26 * East Networks News System is distributed in the hope that it will be useful, 27 * but WITHOUT ANY WARRANTY; without even the implied warranty of 28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 29 * GNU General Public License for more details. 30 31 * You should have received a copy of the GNU General Public License 32 * along with the East Networks News System; if not, write to the Free Software 33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 34 35 * http://www.justhis.com http://ejb.cn 36 * CONTACT: email = webmaster@justhis.com superaxis@sohu.com 37 */ 38 import com.justhis.control.Command; 39 import com.justhis.control.CommandDispatcher; 40 41 import com.justhis.jdo.JDOContext; 42 import com.justhis.jdo.JDOUtil; 43 44 import com.justhis.jibx.JibxUtil; 45 46 import com.justhis.util.XParameters; 47 import com.justhis.util.exception.LogicException; 48 import com.justhis.util.exception.PropertiesFileReadException; 49 import com.justhis.util.exception.UtilException; 50 51 import com.justhis.xml.XMLHelper; 52 53 import net.eastol.news.jdo.bean.NewsSys; 54 import net.eastol.news.jibx.bean.NewsCommonXML; 55 import net.eastol.news.jibx.bean.SystemConfig; 56 57 import org.jibx.runtime.JiBXException; 58 59 import org.w3c.dom.Document; 60 61 import java.io.FileNotFoundException; 62 import java.io.IOException; 63 import java.io.OutputStream; 64 65 import java.sql.SQLException; 66 67 import java.util.Calendar; 68 import java.util.Locale; 69 70 import javax.jdo.PersistenceManager; 71 import javax.jdo.Transaction; 72 73 import javax.servlet.ServletConfig; 74 import javax.servlet.ServletException; 75 import javax.servlet.http.HttpServletRequest; 76 import javax.servlet.http.HttpServletResponse; 77 import javax.servlet.http.HttpSession; 78 79 80 /*** 81 * TODO DOCUMENT ME! 82 * 83 * @author <a href="http://blog.ejb.cn">acai</a> 84 * @version $Revision $ 85 */ 86 public abstract class CommonCommand implements Command { 87 //~ Instance fields -------------------------------------------------------- 88 89 /*** ?????????????????????????????????????????? */ 90 public XParameters xp = null; 91 92 /*** Controller??????dispatcher. */ 93 protected CommandDispatcher dispatcher = null; 94 95 /*** app??????????Document. */ 96 protected Document msgDoc = null; 97 98 /*** request */ 99 protected HttpServletRequest request = null; 100 101 /*** response */ 102 protected HttpServletResponse response = null; 103 104 /*** session */ 105 protected HttpSession session = null; 106 107 /*** JDO?????????????????????????????????? */ 108 protected JDOContext ctx = null; 109 110 /*** ???????? */ 111 protected Locale encoding = null; 112 113 /*** ?????? */ 114 protected OutputStream out = null; 115 116 /*** PersistenceManager ????????????Object */ 117 protected PersistenceManager pm = null; 118 119 /*** ServletConfig .?????????????????? */ 120 protected ServletConfig config = null; 121 122 /*** ????????action */ 123 protected String action = ""; 124 125 /*** Controller?????????????????? */ 126 protected String nextPage = ""; 127 128 /*** TODO */ 129 protected String systemOID = null; 130 131 /*** ???????????????????????? */ 132 protected String warPath = null; 133 134 /*** ??????xslt??????parameters */ 135 protected XParameters xmlParams = null; 136 137 //~ Methods ---------------------------------------------------------------- 138 139 /*** 140 * TODO 141 * 142 * @return TODO 143 * 144 * @throws UtilException TODO 145 */ 146 public SystemConfig getSystemConfig() throws UtilException { 147 try { 148 return (SystemConfig) JibxUtil.unmarshallDocument(SystemConfig.class, 149 warPath 150 + "/config/SystemConfig.xml" 151 ); 152 } catch (JiBXException e) { 153 throw new UtilException(e.getMessage(), e); 154 } 155 } 156 157 /*** 158 * @see com.justhis.control.Command#init(com.justhis.control.CommandDispatcher) 159 */ 160 public void init(CommandDispatcher dispatcher) throws ServletException { 161 this.dispatcher = dispatcher; 162 163 xp = (XParameters) dispatcher.getProperties(); 164 action = xp.getProperty("action"); 165 166 request = dispatcher.getRequest(); 167 config = dispatcher.getServletConfig(); 168 response = dispatcher.getResponse(); 169 session = request.getSession(); 170 171 warPath = config.getServletContext().getRealPath("./"); 172 173 encoding = request.getLocale(); 174 175 systemOID = xp.getProperty("SYSTEM_OID"); 176 177 if ((systemOID == null) || "".equals(systemOID)) { 178 initSystem(); 179 } 180 181 System.out.println("SYTEMOID+==============" + systemOID); 182 } 183 184 /*** 185 * TODO 186 * 187 * @return TODO 188 */ 189 protected synchronized long getDBSequence() { 190 Calendar rightNow = Calendar.getInstance(); 191 long now = rightNow.getTimeInMillis(); 192 193 return now; 194 } 195 196 /*** 197 * TODO 198 * 199 * @return TODO 200 * 201 * @throws ServletException TODO 202 */ 203 protected OutputStream getOuputStream() throws ServletException { 204 // XMLHelper.outputXML(doc, new PrintStream(System.out)); 205 try { 206 return response.getOutputStream(); 207 } catch (IOException e) { 208 e.printStackTrace(); 209 throw new ServletException(e.getMessage()); 210 } 211 } 212 213 /*** 214 * TODO 215 * 216 * @return TODO 217 * 218 * @throws SQLException TODO 219 * @throws LogicException TODO 220 */ 221 protected PersistenceManager getPM() throws SQLException, LogicException { 222 if (this.pm != null) { 223 return pm; 224 } else if ((pm = (PersistenceManager) request.getAttribute("SYSTEM_PM")) != null 225 ) { 226 return pm; 227 } else { 228 try { 229 initSystem(); 230 } catch (ServletException e) { 231 new LogicException(e.getMessage(), e); 232 } 233 } 234 235 return this.pm; 236 } 237 238 /*** 239 * @return TODO 240 */ 241 protected Transaction getTransaction() throws SQLException, LogicException { 242 243 return this.getPM().currentTransaction(); 244 } 245 246 /*** 247 * TODO 248 * 249 * @return TODO 250 */ 251 protected String getXsltFileFromUrl() { 252 String path = request.getServletPath(); 253 String xslFile = path.substring(path.lastIndexOf("/") + 1, 254 path.lastIndexOf(".") 255 ); 256 257 return xslFile + ".xsl"; 258 } 259 260 /* protected void publishXML(Document doc, String xslFileName) 261 throws ServletException, XMLException { 262 this.appendMSGDoc(doc); 263 264 //XMLHelper.outputXML(doc, new PrintStream(System.out)); 265 try { 266 out = response.getOutputStream(); 267 } catch (IOException e) { 268 e.printStackTrace(); 269 throw new ServletException(e.getMessage()); 270 } 271 272 if (encoding != null) { 273 doc.getDocumentElement().setAttribute("locale", encoding.toString()); 274 } 275 276 File xslFile = new File(warPath + "/WEB-INF/xsl/" + xslFileName); 277 278 //????Javax.xml.transformer??transform XML Document 279 Document result = XMLHelper.transformXML(doc, xslFile, this.xmlParams); 280 XMLHelper.outputXML(result, new PrintStream(out)); 281 } 282 */ 283 284 /*** 285 * @param doc 286 */ 287 protected void appendMSGDoc(Document doc) { 288 if (this.msgDoc != null) { 289 XMLHelper.mergeXML(doc.getDocumentElement(), 290 msgDoc.getDocumentElement(), false 291 ); 292 } 293 } 294 295 /*** 296 * TODO 297 * 298 * @param obj TODO 299 * @param xslt TODO 300 * 301 * @throws ServletException TODO 302 * @throws UtilException TODO 303 */ 304 protected void go(Object obj, String xslt) 305 throws ServletException, UtilException { 306 try { 307 JibxUtil.marshallDocument(obj, xslt, this.getOuputStream()); 308 } catch (FileNotFoundException e) { 309 throw new ServletException(e); 310 } catch (JiBXException e) { 311 throw new ServletException(e.getMessage(), e); 312 } 313 } 314 315 /*** 316 * TODO 317 * 318 * @throws ServletException TODO 319 * @throws UtilException TODO 320 */ 321 protected void go() throws ServletException, UtilException { 322 NewsCommonXML ncx = new NewsCommonXML(); 323 324 go(ncx, this.getXsltFileFromUrl()); 325 } 326 327 /*** 328 * TODO 329 * 330 * @param obj TODO 331 * 332 * @throws ServletException TODO 333 * @throws UtilException TODO 334 */ 335 protected void go(Object obj) throws ServletException, UtilException { 336 go(obj, this.getXsltFileFromUrl()); 337 } 338 339 /*** 340 * TODO 341 * 342 * @param ns TODO 343 * 344 * @throws UtilException TODO 345 */ 346 protected void printClassList(NewsSys ns) throws UtilException { 347 try { 348 JibxUtil.marshallDocument(ns, null, 349 warPath + "/config/ClassList.xml" 350 ); 351 } catch (FileNotFoundException e) { 352 throw new UtilException(e.getMessage(), e); 353 } catch (JiBXException e) { 354 throw new UtilException(e.getMessage(), e); 355 } 356 } 357 358 /*** 359 * TODO 360 * 361 * @throws UtilException TODO 362 * @throws SQLException TODO 363 * @throws LogicException TODO 364 */ 365 protected void printClassList() 366 throws UtilException, SQLException, LogicException { 367 NewsSys ns = (NewsSys) JDOUtil.findObjectById(this.getPM(), 368 NewsSys.class, systemOID 369 ); 370 this.printClassList(ns); 371 } 372 373 /*** 374 * TODO 375 * 376 * @throws ServletException TODO 377 */ 378 private void initSystem() throws ServletException { 379 /*** 380 * ?????????????????????? 381 */ 382 try { 383 pm = JDOContext.getContext().getPersistenceManager(); 384 request.setAttribute("SYSTEM_PM", this.pm); 385 } catch (PropertiesFileReadException e) { 386 throw new ServletException(e); 387 } 388 389 try { 390 systemOID = NewsSys.initSystem(this.pm); 391 request.setAttribute("SYSTEM_OID", systemOID); 392 } catch (UtilException e) { 393 System.out.println(" Initial System fail! "); 394 395 throw new ServletException(e); 396 } 397 } 398 } 399 400 401 /* 402 * $Log: CommonCommand.java,v $ 403 * Revision 1.1 2003/09/10 09:28:37 bitiboy 404 * *** empty log message *** 405 * 406 * 407 */

This page was automatically generated by Maven